var common = require("../../utils/Conmon.js");
// pages/setpass/setpass.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    PassNum:'',
    SurePassNum:'',
    telPhone:'',
    num: '', 
    pageUrl: ""
  },

  /**
    * 绑定事件（下一步）
    */
  bindClick: function (e) {
    var that = this;
    if (that.data.SurePassNum.length == 0 || !that.data.SurePassNum)
    {
      wx.showModal({
        title: '注册',
        content: '密码不能为空!',
        showCancel: false
      });
      return;
    }
    if (that.data.PassNum != that.data.SurePassNum)
    {
      wx.showModal({
        title: '注册',
        content: '两次密码不一样!',
        showCancel: false
      });
      return;
    }
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(that.data.telPhone)) {
      wx.showModal({
        title: '注册',
        content: '手机号码格式错误',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            wx.redirectTo({
              url: '/pages/register/register?pageUrl=' + encodeURIComponent(that.data.pageUrl),
            })
          }
        }
      });
      return;
    }
    if (!wx.getStorageSync("unionId"))
    {
      wx.showModal({
        title: '注册',
        content: 'unionId不能为空',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            wx.redirectTo({
              url: '/pages/register/register?pageUrl=' + encodeURIComponent(that.data.pageUrl),
            })
          }
        }
      });
      return;
    }
    that.wxLogin(that.data.telPhone, that.data.num, that.data.PassNum, app.globalData.custID)
  },

  GetPassNum:function(e){
    var PassNum = e.detail.value;
    this.setData({
      PassNum: PassNum
    })
  },
  GetSurePassNum:function(e){
    var SurePassNum = e.detail.value;
    this.setData({
      SurePassNum: SurePassNum
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var telPhone = options.telPhone;
   this.setData({
     telPhone: telPhone,
     pageUrl: decodeURIComponent(options.pageUrl)
   })
   var num = options.num;
   this.setData({
     num: num
   })
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  wxLogin: function (telPhone, num, pwd, custID) {
    //成功检验
    this.wxSubRequest('GET', app.globalData.apiurl + 'member/api/register/RegistUserForSmallApp', {
      phone: telPhone,
      recommendcode: num,
      password: pwd,
      custId: custID,
      memberSources:2,
      unionid: wx.getStorageSync("unionId")
    },
      function (redata) {
        if (redata.code > 0) {//成功
          //成功检验
          try {
            wx.removeStorageSync('IsBindMobile');
          } catch (e) {
            // Do something when catch error
          }
          console.log(redata.content)
          wx.setStorageSync('UserInfo', redata.content);//存储userInfo  
          if (pageUrl && pageUrl != "null")
          {
            wx.switchTab({
              url: pageUrl,
              
            })
          }else
          {
            wx.reLaunch({
              url: "/page/customIndex/index",
            })
          }
        }
        else {
          wx.hideLoading();
          wx.showModal({
            title: '注册',
            content: '注册失败！',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: '/pages/register/register?pageUrl=' + encodeURIComponent(that.data.pageUrl),
                })
              }
            }
          });
        }
      },
      function (obj) {
        wx.hideLoading()
        wx.showModal({
          title: '系统',
          content: '请求失败！',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              wx.redirectTo({
                url: '/pages/register/register?pageUrl=' +encodeURIComponent(that.data.pageUrl),
              })
            }
          }
        });
       
      });
  },
  /**
* 微信提交请求获取数据或保存数据
* 
*/
  wxSubRequest: function (method, url, param, callBack, CallBackError) {
    wx.request({
      url: url,
      method: method,
      data: param,
      header: {
        "content-type": "application/json",
        'X_ManuName': app.globalData.manuName
      },
      success: function (res) {
        if (res.statusCode == 200) {
          var data = res.data;
         
          callBack(data)
          console.log(res.data)
        }
      },
      fail: function (res) {
        CallBackError(res.data)
        console.log(res.data)
      },
      complete: function (res) {
        console.log(res.msg)
      }
    })
  },
})